import sys 
import puzautils.table.puza_stream_table as pst
import puzautils.table.puza_row as pr
import puzautils.table.table as pt

NeighborTable = pst.PuzaStreamTable()
NeighborTable.Read(sys.argv[1])

FilteredTable = pt.PuzaTable()
FilteredTable.Save(sys.argv[2])

while True:
    raw_obj = NeighborTable.StreamInRow()
    if raw_obj != None:
        r = pr.PuzaRow.Load(raw_obj)
        new_row = pr.PuzaRow()
        flag = 0
        try:
            new_row['raw_text'] = r['text']
            new_row['id'] = r['id']
            new_row['user_id'] = r['user']['id']
            new_row['date'] = r['created_at']
            new_row['rt'] = r['retweet_count']
            flag = 1            
        except:
            flag = 0
        if r['retweeted_status'] != None:
            new_row['previous_id'] = r['retweeted_status']['id']
            new_row['previous_text'] = r['retweeted_status']['text']
            new_row['previous_user_id'] = r['retweeted_status']['user']['id']
            new_row['previous_date'] = r['retweeted_status']['created_at']
        if flag == 1:
            FilteredTable.StreamOutRow(new_row, pointer = 0, lformat = 1, block_number = 256)
    else:
        break
FilteredTable.StreamClose()
